// pages/teacher/wode/wode.js
const app = getApp();

Page({
  data: {
    userInfo: null,
    isLoading: true,
    functionList: [
      {
        iconType: 'success_no_circle',
        iconColor: '#4141f3',
        text: '个人设置',
        url: '/pages/shezhi/shezhi'
      },
      {
        iconType: 'warn',
        iconColor: '#ff9f1c',
        text: '修改密码',
        url: '/pages/xiugaimima/xiugaimima'
      },
      {
        iconType: 'waiting',
        iconColor: '#52c41a',
        text: '待办事项',
        url: '/pages/teacher/todo/todo'
      },
      {
        iconType: 'info',
        iconColor: '#13c2c2',
        text: '教学分析',
        url: '/pages/teacher/analysis/analysis'
      }
    ],
    toolsList: [
      {
        iconType: 'success',
        iconColor: '#722ed1',
        text: 'AI助手',
        url: '/pages/ai-chat/ai-chat'
      },
      {
        iconType: 'info',
        iconColor: '#333333',
        text: '关于我们',
        url: '/pages/shezhi/about/about'
      }
    ]
  },

  onLoad: function() {
    this.getUserInfo();
  },
  
  onShow: function() {
    // 每次页面显示时刷新用户信息
    this.getUserInfo();
  },
  
  // 获取用户信息
  getUserInfo: function() {
    wx.request({
      url: 'http://localhost:3000/api/teacher/profile',
      method: 'GET',
      header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('token')
      },
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            userInfo: res.data,
            isLoading: false
          });
        }
      },
      fail: () => {
        // 使用模拟数据
        this.setData({
          userInfo: {
            name: '张教授',
            avatar: '/images/default_avatar.png',
            teacherId: 'T20230001',
            department: '计算机科学与技术学院',
            title: '副教授',
            email: 'zhang@example.com',
            phone: '13800138000',
            courses: 3,
            students: 120
          },
          isLoading: false
        });
      }
    });
  },
  
  // 跳转到功能页面
  navigateTo: function(e) {
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url
    });
  },
  
  // 退出登录
  logout: function() {
    wx.showModal({
      title: '提示',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          // 清除本地存储的token和用户信息
          wx.removeStorageSync('token');
          wx.removeStorageSync('userInfo');
          wx.removeStorageSync('userType');
          
          // 跳转到登录页
          wx.reLaunch({
            url: '/pages/dengluye/dengluye'
          });
        }
      }
    });
  }
});